﻿@using KC.MVC.Core.Constants
@using KC.Web.Account.Constants
@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService
@{
    ViewData["Title"] = "菜单管理";
    Layout = "~/Views/Shared/_ListLayout.cshtml";
    @*菜单角色管理-菜单角色管理*@
    var canRoleInMenu = true;
    @if ((await AuthorizationService.AuthorizeAsync(User, "6862116B-CA67-44DA-9E6A-D5C05DA7F964")).Succeeded)
    {
        canRoleInMenu = true;
    }
}

<div id="toolbar">
    @*菜单管理-保存菜单数据*@
    @if ((await AuthorizationService.AuthorizeAsync(User, "9DFC51A6-E5F9-480C-BB99-D619F9E5E690")).Succeeded)
    {
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-plus" onclick="addMenu()">新增</a>
    }
    @*菜单管理-保存菜单数据*@
    @if ((await AuthorizationService.AuthorizeAsync(User, "9DFC51A6-E5F9-480C-BB99-D619F9E5E690")).Succeeded)
    {
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-pencil" onclick="editMenu()">编辑</a>
    }
    @*菜单管理-删除菜单数据*@
    @if ((await AuthorizationService.AuthorizeAsync(User, "E3A5DD3D-A848-42DE-BE53-C46EE1D0D582")).Succeeded)
    {
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-trash" onclick="removeMenu()">删除</a>
    }

    <div>
        <input id="searchbox" class="easyui-textbox" data-options="prompt:'请输入名称'">
        <a href="javascript:void(0)" onclick="reloadDatagrid()" class="easyui-linkbutton ListToolStartBtn" data-options="iconCls:'fa fa-search'">查询</a>
        <a href="javascript:void(0)" onclick="RefreshSearch()" class="easyui-linkbutton ListToolEndBtn" data-options="iconCls:'fa fa-refresh'">刷新</a>
        @*<a id="closeMenuInRoleBtn" class="easyui-linkbutton" style="margin-left: 5px" onclick="closeMenuInRole()" iconCls="icon-close" href="javascript:void(0)">关闭角色菜单页面</a>*@
    </div>
</div>
<table id="datagrid"></table>
@section scripts
    {
    <script type="text/javascript">

        var canRoleInMenu = '@canRoleInMenu' === 'True';
        $(function() {
            InitTreeDataGrid();
            $('body').css('overflow', 'auto');
        });

        function RefreshSearch() {
            $("#searchbox").searchbox('setValue', '');
            reloadDatagrid();
        }

        function reloadDatagrid() {
            $("#datagrid").treegrid("load", {
                searchValue: $("#searchbox").searchbox("getValue")
            });
        }

        var loadDataUrl = '@Url.Action(ActionName.Menu.LoadMenuList, ControllerName.Menu)';
        function InitTreeDataGrid() {
            $('#datagrid').treegrid({
                url: loadDataUrl,
                method: 'get',
                idField: 'id',
                treeField: 'text',
                striped: true,
                pagination: false,
                fitColumns: true,
                rownumbers: true,
                singleSelect: true,
                nowrap: false,
                fit: true,
                lines: true,
                showFooter: false,
                checkbox: false,
                checkOnSelect: false,
                cascadeCheck: true,
                toolbar: '#toolbar',
                columns: [[
                    { field: 'id', title: '编号', width: 50, align: 'left', hidden: true },
                    { field: 'text', title: '名称', width: 200, align: 'left' },
                    //{ field: 'ControllerName', title: 'Controller', width: 150, align: 'left' },
                    //{ field: 'ActionName', title: 'Action', width: 150, align: 'left' },
                    //{ field: 'URL', title: 'URL', width: 250, align: 'left' },
                    { field: 'Level', title: '层级', width: 50, align: 'left' },
                    { field: 'Index', title: '排序', width: 50, align: 'left' },
                    { field: 'Description', title: '描述', width: 260, align: 'left' },
                    { field: 'oper', title: "操作", width: 150, align: 'left',
                        formatter: function (value, row, index) {
                            var id = row.id;
                            var tdContext = '';
                            if (canRoleInMenu)
                                tdContext += '<a class="btnContacts" style="cursor:pointer" href="#" onclick="addRoleToUser(' + id + ')">分配到角色</a>';
                            return tdContext;
                        }
                    }]
                ],
                onBeforeLoad: function(param) {
                    var a = param;
                },
                onLoadSuccess: function(data) {
                    //$('#datagrid').treegrid('collapseAll');
                    $(".btnContacts").linkbutton({ iconCls: 'fa fa-user' });
                },
                onLoadError: function() {
                }
            });
        }

        var roleInMenu = '@Url.Action(ActionName.Menu.RoleInMenu, ControllerName.Menu)';
        function addRoleToUser(userId, appId) {
            var url = roleInMenu;
            var queryString = "id=" + userId;

            MainPage_PostMessage("openSubPage", url, queryString);
        }

        function searchData(value, name) {
            var appId = $("#searchAppId").combobox('getValue');
            $('#datagrid').datagrid('unselectAll');
            $('#datagrid').treegrid('load', {"searchKey": name, "searchValue": value, "searchAppId": appId});
        }

        function addMenu() {
            opentForm(0);
        }

        function editMenu() {
            var row = $('#datagrid').treegrid('getSelected');
            if (row) {
                opentForm(row.id);
            } else {
                $.messager.showErrorCenter('系统提示', "请选择需要编辑的数据", 3000);
            }
        }

        var getFormUrl = '@Url.Action(ActionName.Menu.GetMenuForm, ControllerName.Menu)';
        var saveFormUrl = '@Url.Action(ActionName.Menu.SaveMenu, ControllerName.Menu)';
        function opentForm(id) {
            $.easyui.showDialog({
                title: id > 0 ? '编辑菜单' : '添加菜单',
                width: 500,
                height: 500,
                modal: true,
                topMost: false,
                href: getFormUrl + "?id=" + id,
                enableHeaderContextMenu: false,
                enableApplyButton: false,
                onSave: function(d) {
                    var validate = d.form("enableValidation").form("validate");
                    if (validate) {
                        $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                        $.ajax({
                            async: true,
                            type: "post",
                            dataType: "json",
                            url: saveFormUrl,
                            data: AddAntiForgeryToken(d.form("getData")),
                            success: function(data) {
                                if (data.success) {
                                    d.window('close');
                                    if (data.Result) {
                                        $('#datagrid').treegrid('clearSelections');
                                        $('#datagrid').treegrid('reload');
                                        $.messager.showInfoCenter('系统提示', '保存数据成功。', 1000);
                                    } else {
                                        $.messager.showErrorCenter('错误消息', '保存数据失败。');
                                    }
                                } else {
                                    $.messager.showErrorCenter('错误消息', data.message);
                                }
                            },
                            complete: function() {
                                $.easyui.loaded();
                            }
                        });
                        return false;
                    } else {
                        return false;
                    }
                }
            });
        }

        var removeUrl = '@Url.Action(ActionName.Menu.RemoveMenu, ControllerName.Menu)';
        function removeMenu() {
            var row = $('#datagrid').treegrid('getSelected');
            if (row) {
                $.messager.confirm('系统提示', '是否确定删除该条记录?', function(r) {
                    if (r) {
                        debugger;
                        $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                        $.post(removeUrl, { id: row.id }, function (data) {
                            debugger;
                            if (data.success) {
                                if (data.Result) {
                                    $('#datagrid').treegrid('clearSelections');
                                    $('#datagrid').treegrid('reload');
                                    $.messager.showInfoCenter('系统提示', '删除数据成功。', 1000);
                                } else {
                                    $.messager.showErrorCenter('错误消息', '删除数据失败。');
                                }
                            } else {
                                $.messager.showErrorCenter('错误消息', data.message);
                            }
                            $.easyui.loaded();
                        });
                    }
                });
            } else {
                $.messager.showErrorCenter('系统提示', "请选择需要删除的数据", 3000);
            }
        }

    </script>
}


